import auth from '@/plugins/auth'
import router, { constantRoutes } from '@/router'
import { staticRoutes, filterStaticRoutes } from '@/router/staticRoutes'
import useUserStore from '@/store/modules/user'

const usePermissionStore = defineStore(
  'permission',
  {
    state: () => ({
      routes: [],
      addRoutes: [],
      defaultRoutes: [],
      topbarRouters: [],
      sidebarRouters: []
    }),
    actions: {
      setRoutes(routes) {
        this.addRoutes = routes
        this.routes = constantRoutes.concat(routes)
      },
      setDefaultRoutes(routes) {
        this.defaultRoutes = constantRoutes.concat(routes)
      },
      setTopbarRoutes(routes) {
        this.topbarRouters = routes
      },
      setSidebarRouters(routes) {
        this.sidebarRouters = routes
      },
      // 设置静态路由
      setStaticRoutes() {
        const userStore = useUserStore()
        const permissions = userStore.permissions || []
        
        // 根据权限过滤静态路由
        const filteredRoutes = filterStaticRoutes(staticRoutes, permissions)
        
        // 设置路由
        this.setRoutes(filteredRoutes)
        this.setSidebarRouters(constantRoutes.concat(filteredRoutes))
        this.setDefaultRoutes(filteredRoutes)
        this.setTopbarRoutes(filteredRoutes)
      }
    }
  }
)

export default usePermissionStore